package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SuifRegister;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.domain.Wallet;
import com.ruoyi.system.mapper.*;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.Suif;
import com.ruoyi.system.service.ISuifService;

/**
 * 信息补充Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-10
 */
@Service
public class SuifServiceImpl implements ISuifService 
{
    @Autowired
    private SuifMapper suifMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private WalletMapper walletMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;


    /**
     * 查询信息补充
     * 
     * @param id 信息补充主键
     * @return 信息补充
     */
    @Override
    public Suif selectSuifById(Long id)
    {
        return suifMapper.selectSuifById(id);
    }

    /**
     * 查询信息补充列表
     * 
     * @param suif 信息补充
     * @return 信息补充
     */
    @Override
    public List<Suif> selectSuifList(Suif suif)
    {
        return suifMapper.selectSuifList(suif);
    }

    /**
     * 新增信息补充
     * 
     * @param suifr 信息补充
     * @return 结果
     */
    @Override
    public int insertSuif(SuifRegister suifr)
    {
        SysUser user = new SysUser();
        user.setUserName(suifr.getUserName());
        user.setPhonenumber(suifr.getPhonebumber());
        user.setPassword(suifr.getPassword());
        user.setRoleId(suifr.getRoleId());
        user.setNickName(suifr.getNickName());
        sysUserMapper.insertUser(user);
        Wallet wallet = new Wallet();
        wallet.setBalance(suifr.getBalance());
        wallet.setUserId(user.getUserId());
        walletMapper.insertWallet(wallet);
        suifr.setUserId(user.getUserId());
        SysUser user1=new SysUser();
        user1.setUserId(user.getUserId());
        user1.setRoleId(user.getRoleId());
        insertUserRole(user1);
        suifr.setNickName(null);
        suifr.setPassword(null);
        suifr.setPhonebumber(null);
        suifr.setBalance(null);
        suifr.setRoleId(null);
        suifMapper.insertSuif(suifr);
        return suifMapper.insertSuif(suifr);
    }

    public void insertUserRole(SysUser user1)
    {
        this.insertUserRole(user1.getUserId(), user1.getRoleIds());
    }

    /**
     * 新增用户角色信息
     *
     * @param userId 用户ID
     * @param roleIds 角色组
     */
    public void insertUserRole(Long userId, Long[] roleIds)
    {
        if (StringUtils.isNotEmpty(roleIds))
        {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>(roleIds.length);
            for (Long roleId : roleIds)
            {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(userId);
                ur.setRoleId(roleId);
                list.add(ur);
            }
            userRoleMapper.batchUserRole(list);
        }
    }

    /**
     * 修改信息补充
     * 
     * @param suif 信息补充
     * @return 结果
     */
    @Override
    public int updateSuif(Suif suif)
    {
        return suifMapper.updateSuif(suif);
    }

    /**
     * 批量删除信息补充
     * 
     * @param ids 需要删除的信息补充主键
     * @return 结果
     */
    @Override
    public int deleteSuifByIds(Long[] ids)
    {
        return suifMapper.deleteSuifByIds(ids);
    }

    /**
     * 删除信息补充信息
     * 
     * @param id 信息补充主键
     * @return 结果
     */
    @Override
    public int deleteSuifById(Long id)
    {
        return suifMapper.deleteSuifById(id);
    }

    @Override
    public Suif selectUserName(String userName) {
        return suifMapper.selectUserName(userName);
    }
}
